<?php

/**
 *	  资金流向：收入
 *    支出类型：卖出商品
 *
 *    @author   psmb
 *    @usage    none
 */
class SellgoodsIncome extends IncomeDepopay
{
    var $_purpose_name = 'sellgoods';
	
	
	function submit($data)
	{
		/* 释放trade_info和extra_info和post三个变量 */
        extract($data);
        /* 处理交易基本信息 */
        $base_info = $this->_handle_trade_info($trade_info, $post);
		$order_info = $this->_handle_order_info($extra_info);
        if (!$base_info || !$order_info)
        {
            /* 基本信息验证不通过 */
            return false;
        }
		
		/* 通过order_sn 找出 tradesn */
		if(!$tradesn = $this->_get_trade_sn($extra_info['order_sn'])) {
			$this->_errorCode[] = "50010";
			return false;
		}
		
		/* 插入收支记录，并变更账户余额，变更买家收支记录状态，插入卖家收支记录 */
		if(!$this->_insert_record_info($tradesn, $trade_info, $extra_info)) {
			$this->_errorCode[] = "50008";
			return false;
		}
		
		/* 如果有交易服务费，则扣除卖家手续费 */
		if($trade_rate = $this->_get_deposit_setting($trade_info['user_id'], 'trade_rate')) {
			if(!parent::_sys_chargeback($tradesn, $trade_info, $trade_rate, 'trade_fee')) {
				$this->_errorCode[] = "50009";
				return false;
			}
		}
		
		return $tradesn;
	}
	
	/* 插入收支记录，并变更账户余额 */
	function _insert_record_info($tradesn, $trade_info, $extra_info)
	{
		$time = gmtime();
		$deposit_record_mod = &m('deposit_record');
		
		/* 变更买家的收支记录状态 */
		if($extra_info['change_order_status'] === true) {
			$deposit_record_mod->edit("order_sn='".$extra_info['order_sn']."' AND user_id=".$trade_info['party_id'], array('status'=>'SUCCESS','end_time'=>$time));
		}
		
		$data_record = $deposit_record_mod->get("order_sn='".$extra_info['order_sn']."' AND user_id=".$trade_info['party_id']);
		unset($data_record['record_id']);
			
		// 增加卖家的收支记录
		$data_record['user_id']	=	$trade_info['user_id']; // 卖家ID
		$data_record['party_id']=	$trade_info['party_id'];
		$data_record['amount']  =   $trade_info['amount'];
		$data_record['balance']	=	$this->_get_deposit_balance($trade_info['user_id'], $trade_info['amount']); // 增加后的余额
		$data_record['flow']	=	$this->_flow_name;
		$data_record['purpose']	=	$this->_purpose_name;
		$data_record['status']  = 	'SUCCESS';
		$data_record['end_time']=   $time;
			
		/* 插入卖家的收支记录 */
		return parent::_insert_deposit_record($data_record);
	}
	
	
	
	function _get_trade_sn($order_sn)
	{
		$deposit_record_mod  = &m('deposit_record');
		$depoist_record = $deposit_record_mod->get(array('conditions'=>"order_sn='".$order_sn."' AND status='SHIPPED' ",'fields'=>'tradesn'));
		
		$tradesn = $depoist_record['tradesn'];
		
		return $tradesn;
	}
	
	
}

?>
